<template>
<div class="block" style="margin:0 0 30px 75px;">
    <span class="demonstration" style="margin-right:5px;font-size:14px;width: 85px;display: inline-block;text-align: right;">办结时间</span>
    <el-date-picker v-model="overtime" @change="overChange" size="mini" value-format="yyyy-MM-dd" type="date" placeholder="选择日期">
    </el-date-picker><span class="el-upload__tip">({{enday}}天后)</span>
</div>
</template>
<script>
  export default {
    data () {
      return {
        enday:0,
        overtime:'',
        year:'',
        month:'',
        today:''
      }
    },
    props:{
        times:{
            type:String
        }
    },
    watch:{
        times(newValue, oldValue) {
            console.log(newValue,oldValue);
            this.overtime = newValue;
            // var ddd = new Date(newValue);
            // var month = ddd.getMonth() + 1;
            // var day = ddd.getDate();
            // var year = ddd.getFullYear();
            // if (month == 0) {
            //     month = 12;
            //     year = year - 1;
            //     }
            //     if (month < 10) {
            //     month = addZero(month);
            // }
            // this.overtime = year + "-" + month + "-" + day;
            this.enday = Math.ceil( ( parseInt( Date.parse(newValue)-Date.parse( new Date() ) )/(3600*24*1000) ) );
        } 
    },
    mounted() {
      
    },
    created() {
        // console.log(this.times);
        //设置默认截止日期
        function addZero(n) {
        return n < 10 ? "0" + n : "" + n;
        }
        // var newday = Date.parse(new Date())+60*60*24*7*1000;
        var ddd = new Date(new Date());

        var month = ddd.getMonth() + 1;
        var day = ddd.getDate();
        var year = ddd.getFullYear();
        if (month == 0) {
        month = 12;
        year = year - 1;
        }
        if (month < 10) {
        month = addZero(month);
        }
        this.year=year;
        this.month=month;
        this.today=day;

        this.overtime = year + "-" + month + "-" + day;
    },
    methods: {
        getContent() { // 获取方法
            return this.overtime;
        },
        overChange(val){
            if( parseInt( Date.parse(val)-Date.parse( new Date() ) )>0){
                this.enday =Math.ceil( ( parseInt( Date.parse(val)-Date.parse( new Date() ) )/(3600*24*1000) ) );
            }else{
                this.$message.error("只能选择"+this.year + '-'+this.month+'-'+this.today+"以后的日期");
                this.overtime = this.year + '-'+this.month+'-'+this.today;
                this.enday = 0;

            }
        },
    },
    destroyed() {
        this.enday = 0;
    }
  }
</script>